package com.asia5b.a5bmall.same.fragment

import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.asia5b.a5bmall.same.R
import com.asia5b.a5bmall.same.adapter.GoodsCouponAdapter
import com.asia5b.a5bmall.same.databinding.DialogGoodscouponBinding
import com.asia5b.a5mall.middleware.base.SimpleDialogFragment
import com.asia5b.a5mall.middleware.logic.viewmodel.goods.GoodsCouponDFVM
import com.asia5b.a5mall.middleware.model.member.CouponModel
import com.asia5b.a5mall.middleware.model.member.PromotionModel
import com.asia5b.a5mall.middleware.utils.judge
import com.chad.library.adapter.base.entity.MultiItemEntity
import kotlinx.android.synthetic.main.dialog_goodscoupon.*

/**
 * Created by zhangyuncai on 2020/1/22.
 */
class GoodsCouponDF : SimpleDialogFragment<GoodsCouponDFVM, DialogGoodscouponBinding>(){
    override val viewModelClazz: Class<GoodsCouponDFVM>
        get() = GoodsCouponDFVM::class.java

    override fun getDataBinding(container: ViewGroup?): DialogGoodscouponBinding {
        return DialogGoodscouponBinding.inflate(layoutInflater,container,false)
    }
    val mRecyclerView by lazy { rv_couponlist }
    val mDatas = arrayListOf<MultiItemEntity>()
    val mAdapter by lazy { GoodsCouponAdapter(mDatas) }
    var type = 1


    override fun initView() {
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }
        tv_goodscoupon_title.text = (type == 1).judge(getString(R.string.优惠券), getString(R.string.活动))
    }

    override fun initData() {
    }

    override fun initEvent() {

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = mAdapter.getItem(position) as MultiItemEntity
            if (item is CouponModel) {
                val model = item as CouponModel
                when (view.id) {
                    R.id.tv_couponlist_use_now,
                    R.id.tv_couponlist_shopname -> {
                        mViewModel.receiveCoupon(model.coupon_id)
                    }
                }
            } else if (item is PromotionModel) {
                val model = item as PromotionModel
            }
        }
    }


    override val heightPercent: Double
        get() = 0.7
}